﻿using Benefactor.Models.DbModels;
using Bigway.Common.Services.Repositories;

namespace Benefactor.Services.Repositories;

public class CrmebProductRepository(IServiceProvider sp, IAppConfig appConfig) 
    : BaseCrmebRepository<CrmebProduct>(sp), ICrmebProductRepository
{
    public async Task<List<CrmebProduct>> GetAllBenefitsCardStoreProducts(
        IList<int>? productIds,
        CancellationToken token = default)
    {
        var merchantId = int.Parse(appConfig.BenefitsCardMerchantId);
        var products = await GetAllAsync(
            query =>
            {
                if (productIds is {Count: > 0})
                    query = query.Where(x => productIds.Contains(x.Id));
                return query.Where(x => x.IsDeleted == 0 && x.MerchantId == merchantId)
                    .OrderByDescending(x => x.CreateTime)
                    .Includes(x => x.AttrValues);
            }, 
            token
        );

        return products;
    }
}